package fnord.jovian.tango;

import java.awt.*;
import java.awt.geom.*;

public class TheButton implements TheObject {

	protected RectangularShape _shape;
	protected TheText _title;
	protected int _xPadding = 6, _yPadding = 6;
	protected Color _background, _out = Color.BLACK, _over = new Color(30,30,30), _click = new Color(40,40,40);
	protected TheListener _listener = null;
	protected boolean _active = true;

	public TheButton(String title) {
		_title = new TheText(title,12,0,0,true,true);
		_shape = new RoundRectangle2D.Double();
		_background = _out;
	}

	public TheButton(String title, double w, double h) {
		_title = new TheText(title,12,w,h,true,true);
		_shape = new RoundRectangle2D.Double(0,0,w,h,20,20);
		_background = _out;
	}

	public TheButton(String title, double w, double h, TheListener listener) {
		_title = new TheText(title,12,w,h,true,true);
		_shape = new RoundRectangle2D.Double(0,0,w,h,20,20);
		_background = _out;
		_listener = listener;
	}

	public TheButton(String title, double w, double h, TheListener listener, double fontSize) {
		_title = new TheText(title,fontSize,w,h,true,true);
		_shape = new RoundRectangle2D.Double(0,0,w,h,20,20);
		_background = _out;
		_listener = listener;
	}

	public void paint(Graphics2D g) {
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(_background);
		g.fill(_shape);
		g.setColor(Color.WHITE);
		_title.setLocation(getX(), getY());
		_title.setSize(getWidth(), getHeight());
		_title.paint(g);
	}
	
	public void setColor(Color color) {
		_background = color;
		_out = color;
	}

	public void setSize(double width, double height) {
		_shape.setFrame(_shape.getX(), _shape.getY(), width, height);
		_title.setLocation(width, height);
	}

	public void setLocation(double x, double y) {
		_shape.setFrame(x, y, _shape.getWidth(), _shape.getHeight());
		_title.setLocation(x, y);
	}

	public void setPadding(int xPadding, int yPadding) {
		_xPadding = xPadding;
		_yPadding = yPadding;
	}

	public void setTheListener(TheListener listener) {
		_listener = listener;
	}

	@Override
	public boolean contains(Point p) {
		if (_active) {
			if (_shape.contains(p)) {
				containAction();
				return true;
			}
			else {
				outAction();
				return false;
			}
		}
		else
			return false;
	}

	public boolean containsUp(Point p) {
		if (_active) {
			if (_shape.contains(p)) {
				upAction();
				return true;
			}
			else {
				outAction();
				return false;
			}
		}
		else
			return false;
	}

	public void containAction() {
		_background = _over;
	}

	public void clickAction() {
		_background = _click;
	}

	public void upAction() {
		_background = _out;
		if (_listener != null)
			_listener.doAction();
	}

	public void outAction() {
		_background = _out;
	}

	public double getHeight() {
		return _shape.getHeight();
	}

	public double getWidth() {
		return _shape.getWidth();
	}

	public double getX() {
		return _shape.getX();
	}

	public double getY() {
		return _shape.getY();
	}

	@Override
	public boolean containsClick(Point p) {
		if (_active) {
			if (_shape.contains(p)) {
				clickAction();
				return true;
			}
			else {
				outAction();
				return false;
			}
		}
		else
			return false;
	}

	@Override
	public boolean containsDrag(Point p) {
		if (_active) {
			if (_shape.contains(p)) {
				dragAction();
				return true;
			}
			else {
				outAction();
				return false;
			}
		}
		else
			return false;
	}

	public void dragAction() {
	}

	@Override
	public double getCenterX() {
		// TODO Auto-generated method stub
		return _shape.getCenterX();
	}

	@Override
	public double getCenterY() {
		// TODO Auto-generated method stub
		return _shape.getCenterY();
	}

	public void setText(String text) {
		_title.setText(text);
	}
	
	public void setActive(boolean active) {
		_active = active;
	}
}
